package com.webserver.controller;

import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与文章相关的业务
 */
public class ArticleController {
    private static File ARTICLE_DIR = new File("./articles");

    static {
        if (!ARTICLE_DIR.exists()) {
            ARTICLE_DIR.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章...");
        //获取文章信息
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);

        if (title == null || author == null || content == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file);
            return;
        }

        File articleFile = new File(ARTICLE_DIR, title + ".obj");
        if (articleFile.exists()) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file);
            return;
        }

        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleFile)
                );
        ) {
            Article article = new Article(title, author, content);
            oos.writeObject(article);
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File("./webapps/myweb/article_success.html");
        response.setContentFile(file);
        System.out.println("发表文章处理完毕!");
    }

    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {

        List<Article> articleList = new ArrayList<>();
        File[] subs = ARTICLE_DIR.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFile : subs) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(articleFile)
                    );
            ) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        //2将所有文章拼接到一个html页面中
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");

        for (Article article : articleList) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }

        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");
    }

}





